//
//  HMStatusOriginalView.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/24.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit
import SVProgressHUD
import SDWebImage
import SnapKit

let StatusCellMargin:CGFloat = 8


class MYStatusOriginalView: UIView {
    
    //底部约束
    var bottomCons: Constraint?
    
    
    var statusViewModel:MYStatusViewModel? {
        
        didSet {
            
            
            //加载头像图片
            headIcon.sd_setImageWithURL(statusViewModel?.headIconURL)
            //设置头像圆角
            headIcon.cornerRadius = 20
            headIcon.layer.masksToBounds = true
            
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.status?.created_at
            sourceLabel.text = statusViewModel?.status?.source
            contentLabel.text = statusViewModel?.status?.text
            
            //设置认证图标
            verifiedImageView.image = statusViewModel?.verifiedImage
            //设置微博等级图标
            mbrankImageView.image = statusViewModel?.mbrankImage
            //发布时间
            timeLabel.text = statusViewModel?.time
            //来源
            sourceLabel.text = statusViewModel?.souseText
            
            //注销约束
            bottomCons?.uninstall()
            //如果存在图片视图
            if let pics = statusViewModel?.status?.pic_urls where pics.count > 0 {
                
                pictureView.hidden = false
                //MARK:这里重新设置了图片视图的大小,更新了视图(重走数据源)
                pictureView.pictures = pics
                
                //重新给转发微博视图设置底部约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                pictureView.pictures = nil
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            }

        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        //测试背景颜色
        backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //布局子控件
    private func setupUI() {
        addSubview(sepView)
        addSubview(headIcon)
        addSubview(verifiedImageView)
        addSubview(mbrankImageView)
        addSubview(nameLabel)
        addSubview(sourceLabel)
        addSubview(timeLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        sepView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        headIcon.snp_makeConstraints { (make) in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(sepView.snp_left).offset(StatusCellMargin)
            make.width.equalTo(40)
            make.height.equalTo(40)
        }
        
        verifiedImageView.snp_makeConstraints { (make) in
            make.bottom.right.equalTo(headIcon)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            make.top.equalTo(headIcon.snp_top)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(headIcon.snp_top)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_left)
            make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
        }
        
        self.snp_makeConstraints { (make) in
           self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
    }
    
    
    
    
    //MARK:懒加载子控件
    
    ///分割线
    private lazy var sepView: UIView = {
        let line = UIView()
        line.backgroundColor = UIColor(white: 0.85, alpha: 1)
        return line
    }()
    ///头像
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    ///认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    ///等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    ///名字
    private lazy var nameLabel: UILabel = UILabel(title: "午夜涂涂", font: 14.0, textColor: UIColor.darkGrayColor(), alignment: .Left)
    ///微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "来自 微博weibo.com", font: 10, textColor: UIColor.darkGrayColor())
    ///发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "00:00", font: 10, textColor: UIColor.orangeColor())
    ///正文
    private lazy var contentLabel: UILabel = UILabel(title: "新浪微博: 随时随地发现新鲜事", font: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    ///图片视图
    private var pictureView: MYStatusPictureView = MYStatusPictureView()


}
